package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.service.DataAnalysisService;
import edu.ynu.se.xiecheng.achitectureclass.service.DataAnalysisService.GreenhouseDataSummary;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Api(tags = "数据分析接口")
@RestController
@RequestMapping("/api/analysis")
@RequiredArgsConstructor
public class DataAnalysisController {
    private final DataAnalysisService dataAnalysisService;

    @ApiOperation("获取大棚数据摘要")
    @GetMapping("/summary/{greenhouseId}")
    public ResponseEntity<GreenhouseDataSummary> getGreenhouseDataSummary(@PathVariable Long greenhouseId) {
        GreenhouseDataSummary summary = dataAnalysisService.getGreenhouseDataSummary(greenhouseId);
        return ResponseEntity.ok(summary);
    }

    @ApiOperation("获取设备状态")
    @GetMapping("/status/{greenhouseId}")
    public ResponseEntity<Map<String, String>> getDeviceStatus(@PathVariable Long greenhouseId) {
        Map<String, String> status = dataAnalysisService.getDeviceStatus(greenhouseId);
        return ResponseEntity.ok(status);
    }

    @ApiOperation("获取数据趋势")
    @GetMapping("/trend/{greenhouseId}")
    public ResponseEntity<Map<String, List<Map<String, Object>>>> getDataTrend(
            @PathVariable Long greenhouseId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        Map<String, List<Map<String, Object>>> trend = dataAnalysisService.getDataTrend(greenhouseId, startTime, endTime);
        return ResponseEntity.ok(trend);
    }
} 